package bc

import (
	"fmt"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bc"
	bcReq "github.com/flipped-aurora/gin-vue-admin/server/model/bc/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BcAgencyApi struct {
}

var bcAgencyService = service.ServiceGroupApp.BcServiceGroup.BcAgencyService

// CreateBcAgency 创建bcAgency表
// @Tags BcAgency
// @Summary 创建bcAgency表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcAgency true "创建bcAgency表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /bcAgency/createBcAgency [post]
func (bcAgencyApi *BcAgencyApi) CreateBcAgency(c *gin.Context) {
	var bcAgency bc.BcAgency
	err := c.ShouldBindJSON(&bcAgency)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcAgencyService.CreateBcAgency(&bcAgency); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBcAgency 删除bcAgency表
// @Tags BcAgency
// @Summary 删除bcAgency表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcAgency true "删除bcAgency表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /bcAgency/deleteBcAgency [delete]
func (bcAgencyApi *BcAgencyApi) DeleteBcAgency(c *gin.Context) {
	var bcAgency bc.BcAgency
	err := c.ShouldBindJSON(&bcAgency)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcAgencyService.DeleteBcAgency(bcAgency); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBcAgencyByIds 批量删除bcAgency表
// @Tags BcAgency
// @Summary 批量删除bcAgency表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除bcAgency表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /bcAgency/deleteBcAgencyByIds [delete]
func (bcAgencyApi *BcAgencyApi) DeleteBcAgencyByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcAgencyService.DeleteBcAgencyByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBcAgency 更新bcAgency表
// @Tags BcAgency
// @Summary 更新bcAgency表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body bc.BcAgency true "更新bcAgency表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /bcAgency/updateBcAgency [put]
func (bcAgencyApi *BcAgencyApi) UpdateBcAgency(c *gin.Context) {
	var bcAgency bc.BcAgency
	err := c.ShouldBindJSON(&bcAgency)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bcAgencyService.UpdateBcAgency(bcAgency); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBcAgency 用id查询bcAgency表
// @Tags BcAgency
// @Summary 用id查询bcAgency表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bc.BcAgency true "用id查询bcAgency表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /bcAgency/findBcAgency [get]
func (bcAgencyApi *BcAgencyApi) FindBcAgency(c *gin.Context) {
	var bcAgency bc.BcAgency
	err := c.ShouldBindQuery(&bcAgency)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rebcAgency, err := bcAgencyService.GetBcAgency(bcAgency.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebcAgency": rebcAgency}, c)
	}
}

// GetBcAgencyList 分页获取bcAgency表列表
// @Tags BcAgency
// @Summary 分页获取bcAgency表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query bcReq.BcAgencySearch true "分页获取bcAgency表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bcAgency/getBcAgencyList [get]
func (bcAgencyApi *BcAgencyApi) GetBcAgencyList(c *gin.Context) {
	var pageInfo bcReq.BcAgencySearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	data, err := utils.GetClaims(c)
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}
	list := make([]string, 0)
	switch data.AuthorityId {
	case 8881:
		list = append(list, data.SettleOffice)
	}

	if list, total, err := bcAgencyService.GetBcAgencyInfoList(pageInfo, list); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		if data.AuthorityId == 1 {
			list = make([]bc.BcAgency, 0)
		}
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func (bcAgencyApi *BcAgencyApi) DeleteBcAgencyByTime() {
	fmt.Println("开始批量删除代理数据记录....")
	var IDS request.IdsReq
	list, err := bcAgencyService.GetBcAgencyExpireList()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		return
	}
	for b := 0; b < len(list); b++ {
		IDS.Ids = append(IDS.Ids, int(list[b].ID))
	}
	if err := bcAgencyService.DeleteBcAgencyByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		return
	} else {
		fmt.Println("批量删除成功")
	}
}
